// pages/interview_invitation/interview_invitation.js
const app = getApp(), core = require('../../utils/core'), util = require('../../utils/util'), QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js'), mapsdk = new QQMapWX({key: app.globalData.mapSdkKey}),chatIM = require('../../utils/websocket.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabNumber:{tab_0: 0, tab_1: 0, tab_2: 0, tab_3: 0, tab_4: 0},
    status:0,
    interviews:[],
    interviewsMore: {page:1},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.tabNumber();
	this.fetch();
	//未读消息
	if (app.globalData.chat_config.SocketTask) {
		chatIM.setMessageBadge(false)
	 }
	app.eventBus.on(app.Events.WebsocketMessageCount, this.eventWebsocketMessageCount,this);
  },

  onShow: function(){
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar() ) {
      this.getTabBar().setData({
        selected: 3
      })
    }
  },
  onUnload(){
	app.eventBus.off(app.Events.WebsocketMessageCount, this.eventWebsocketMessageCount);
  },
  changeTab(e){
      this.setData({status: e.currentTarget.dataset.status});
      this.refresh();
  },

  refresh(){
    this.setData({interviews:[], interviewsMore:{page:1}});
    this.tabNumber();
    this.fetch();
  },

   /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.refresh();
    wx.stopPullDownRefresh();
  },

  /**
   * 职位详情
   */
  job_detail:function(e){
      wx.navigateTo({
        url: '/pages/jobs_detail/jobs_detail?id='+e.currentTarget.dataset.id,
      })

  },

  bindShowMap(event) {
    let index = event.currentTarget.dataset.index;
    let interview = this.data.interviews[index];
    if(interview.lat) {
      wx.openLocation({
        latitude: interview.lat , longitude: interview.lng, address: interview.address
      })
    }
    if(interview.lat === undefined) {
      wx.showLoading({title:'',mask:true});
      mapsdk.geocoder({
        address: interview.address, sig: app.globalData.mapSdkSk,
        success: (res, data) => {
          wx.hideLoading();
          if(res.status===0) {
            interview.lat = res.result.location.lat;
            interview.lng = res.result.location.lng;
            wx.openLocation({
              latitude: interview.lat , longitude: interview.lng, address: interview.address
            });
          }else {
            interview.lat = null;
            wx.showToast({title: '无效地址,无法打开地图', icon: 'none'});
          }
        }, fail: (res, data) => {
          wx.hideLoading();
          interview.lat = null;
          wx.showToast({title: '无效地址,无法打开地图', icon: 'none'});
        }
      })
    }else {
      wx.showToast({title: '无效地址,无法打开地图', icon: 'none'});
    }

  },

  bindRejectInterview(event) {
    let index = event.currentTarget.dataset.index;
    let interview = this.data.interviews[index];
    wx.showModal({
      title: '提示',
      content: '确认拒绝面试？',
      success: res=>{
        if (res.confirm) {
          core.post({
            url: 'xilujob.job_interview/multi_status/interview_id/'+interview.id, data: {status:1},
            success: ret => {
              //this.setData({['interviews['+index+'].status']: ret.data.status});
              this.refresh();
            }
          });
        }
      }
    })
  },
  bindAcceptInterview(event) {
    let index = event.currentTarget.dataset.index;
    let interview = this.data.interviews[index];
    wx.showModal({
      title: '提示',
      content: '确认接受面试？',
      success: res=>{
        if (res.confirm) {
          core.post({
            url: 'xilujob.job_interview/multi_status/interview_id/'+interview.id, data: {status:2},
            success: ret => {
              this.refresh();
              //this.setData({['interviews['+index+'].status']: ret.data.status});
              // wx.requestSubscribeMessage({
              //   tmplIds: ['Yev8H7fyuRxRGzbn8Kbu6d3g7qjSSOyGenYhPh_Gg_w'], //订阅 面试成功 消息； 面试失败模板未通过
              //   success: res => {
              //     console.log('success', res);
              //   },
              //   fail: res => {
              //     console.log('fail', res);
              //   }
              // });
            }
          });
        }
      }
    })
  },

  fetch() {
    var status = this.data.status;
    util.fetch(this, '/xilujob.job_interview/personal_list', {status:status}, 'interviewsMore', 'interviews', '');
  },

  tabNumber() {
    var page = this;
    core.get({url: '/xilujob.job_interview/personal_list_count',success(ret,response){page.setData({tabNumber:ret.data})}});
  },
  eventWebsocketMessageCount(data){
	var list = this.getTabBar().data.list;
	list[1].megNum = app.globalData.chat_config.messageCount;
	this.getTabBar().setData({
	  list
	})
  },
});
